<?php
/**
 * Created by PhpStorm.
 * User: leo
 * Date: 16/4/17
 * Time: 下午6:33
 * Describe:
 */

namespace services\models;

use \common\models\OrderDiscount as OD;
use Yii;

class OrderDiscount extends \yii\base\Component
{

    /**
     * 促销优惠入口
     * @param Order $order
     */
    public static function discount(&$order, $save = false)
    {

        static::firstExpressFree($order, $save);
        static::FreeShipTag($order);
        $fc = new \services\models\discount\FullCut();
        $fc->getDiscountMoney($order);


        static::NineDotNineFreeExpress($order);

        $order->try_coin += $order->ship_amount_try_coin;
        $order->order_amount =
            $order->goods_amount - $order->reduce_amount + $order->ship_amount_discount - $order->member_money;

        if ($order->ship_amount_discount == 0) {
            $order->ship_amount_try_coin = 0;
        } else {
            if ($order->use_coin_for_ship_amount) {



                if ($order->use_coin_for_ship_amount == 1 && $order->ship_amount_discount >0) {
                    if ($order->score > $order->ship_amount_try_coin + $order->try_coin) {
                        if ($order->score < $order->try_coin + $order->ship_amount * $order->money_convery_to_money) {
                            $order->addError('ship_amount_try_coin', '试豆不够抵扣邮费');
                        } else {
                            $order->ship_amount_try_coin = $order->ship_amount * $order->money_convery_to_money;
                            $order->try_coin += $order->ship_amount_try_coin;
                        }

                    }
                }
                $order->order_amount -= $order->ship_amount_discount;
                $order->ship_amount_discount = 0;
            }
        }

        if ($order->order_amount < 0) {
            $order->addError('ship_amount_try_coin', '订单金额无需扣除');
        }




    }

    /**
     * @param Order $order
     * @param bool  $save
     */
    public static function NineDotNineFreeExpress(&$order)
    {
        if (($order->goods_amount - $order->reduce_amount) >= 9.9 && $order->ship_amount > 0 && $order->ship_amount_discount === $order->ship_amount) {
            $order->ship_amount_discount = 0;
            if ($order->use_coin_for_ship_amount == 1) {
                $order->try_coin -= $order->ship_amount_try_coin;
                $order->ship_amount_try_coin = 0;
            }
            $discount                 = new OD();
            $discount->discount_money = $order->ship_amount;
            $discount->type           = 'FreeShip 9.9';
            $discount->describe       = '9.9包邮';
            $discount->type_id        = 0;
            $order->addDiscountMsg($discount);
        }
    }

    /**
     * 首单免邮
     * @param Order $order
     * @param       $save
     */
    public static function firstExpressFree(&$order, $save)
    {

        /** @var Order $order */
        /** @var Promotion $pt */
        $pt = Promotion::find()->byBestryUid($order->bestry_uid);
        if ($order->ship_amount > 0 && $order->ship_amount_discount === $order->ship_amount) {

            if ($pt !== null && $pt->first_express_free !== 0) {
                return;
            }
            if ($order->use_coin_for_ship_amount == 1) {
                #$order->try_coin -= $order->ship_amount_try_coin;
                $order->ship_amount_try_coin = 0;
            }
            $order->ship_amount_discount = 0;
            $discount                    = new OD();
            $discount->discount_money    = $order->ship_amount;
            $discount->type              = 'FreeShip';
            $discount->describe          = '首单免邮';
            $discount->type_id           = 0;
            $order->addDiscountMsg($discount);


        }
        if ($save) {
            if ($pt !== null && $pt->first_express_free !== 0)
                return;

            if ($pt === null) {
                $pt                     = new Promotion();
                $pt->bestry_id          = $order->bestry_uid;
                $pt->first_express_free = 1;


            } else {
                $pt->first_express_free = 1;
            }
            $pt->save();
        }
    }


    public static function FreeShipTag(&$order)
    {
        if ($order->ship_amount > 0 && $order->ship_amount_discount === $order->ship_amount) {

            foreach ($order->ProductsInCart as $product) {
                if ($product->goodsDetail->free_post !== GoodsDetail::$FREE_POST_YES) {
                    return;
                }
            }

            if ($order->use_coin_for_ship_amount == 1) {
                $order->try_coin -= $order->ship_amount_try_coin;
                $order->ship_amount_try_coin = 0;
            }
            $order->ship_amount_discount = 0;
            $discount                    = new OD();
            $discount->discount_money    = $order->ship_amount;
            $discount->type              = 'FreeShip';
            $discount->describe          = '免邮费商品';
            $discount->type_id           = 0;
            $order->addDiscountMsg($discount);
        }

    }

}